import asyncio
from pyppeteer import launch

width, height = 1366, 768


async def main():
    browser = await launch(headless=False, args=['--disable-infobars', f'--window-size={width},{height}'])
    page = await browser.newPage()
    await page.setViewport({'width': width, 'height': height})
    await page.goto('https://demo.mycodes.net/daima/suopingjiesuo/')
    await page.screenshot({'path':'图片.png'})
    # 一些网站主要通过 window.navigator.webdriver 来对 webdriver 进行检测，所以我们只需要使用 JavaScript 将它设置为 false 即可
    await page.evaluate('''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')

    btn_position = await page.evaluate('''
           () =>{
            return {
             x: document.querySelector('canvas.main-c').getBoundingClientRect().x,
             y: document.querySelector('canvas.main-c').getBoundingClientRect().y,
             width: document.querySelector('canvas.main-c').getBoundingClientRect().width,
             height: document.querySelector('canvas.main-c').getBoundingClientRect().height
             }}
            ''')
    x = btn_position['x'] - 50
    y = btn_position['y'] + 30

    await page.hover('canvas.main-c')

    # await page.mouse.down({'button':'right'})
    # await page.mouse.up({'button': 'right'})
    await page.mouse.down()
    await page.mouse.move(603, 247, {'steps': 30})

    await page.mouse.move(703, 247, {'steps': 30})

    await page.mouse.move(773, 247, {'steps': 30})

    await page.mouse.move(773, 337, {'steps': 30})

    await page.mouse.move(773, 407, {'steps': 30})

    await page.mouse.move(703, 407, {'steps': 30})

    await page.mouse.move(603, 407, {'steps': 30})

    await page.mouse.move(603, 337, {'steps': 30})

    # await page.mouse.move(703, 337, {'steps': 30})

    await page.mouse.up()
    # await page.mouse.down({'button':'right'})
    # await page.mouse.up({'button': 'right'})
    await asyncio.sleep(100)

asyncio.get_event_loop().run_until_complete(main())